package com.zhangc.blog.admin.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.zhangc.blog.admin.entity.BlogInfoEntity;
import com.zhangc.blog.admin.entity.UserEntity;
import com.zhangc.blog.admin.service.RedisService;
import com.zhangc.blog.admin.service.UserService;
import com.zhangc.blog.base.BaseException;
import com.zhangc.blog.base.MessageSourceUtils;
import com.zhangc.blog.base.ResultBO;
import com.zhangc.blog.base.enums.BlogExceptionCode;
import com.zhangc.blog.base.validator.Validators;

/**
 * 功能描述:<br>
 *
 * @author wx:fdzhangc
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@RestController
@RequestMapping("/test")
public class HelloTestController {
    private static final Logger logger = LoggerFactory.getLogger(HelloTestController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    @RequestMapping("/setRedisList")
    @ResponseBody
    public String setRedisList() {
        BlogInfoEntity blog = new BlogInfoEntity();
        blog.setBlogTitle("111");
        List<BlogInfoEntity> blogList = new ArrayList<>();
        blogList.add(blog);
        redisService.set("zcblog:hotblogs", blogList);
        return "success";
    }

    @RequestMapping("/getRedisList")
    @ResponseBody
    public Object getRedisList() throws JsonProcessingException {
        return redisService.get("zcblog:hotblogs");
    }

    @RequestMapping("/getObj")
    @ResponseBody
    public Object getObj() throws JsonProcessingException {
        return redisService.get("zcblog:blog1");
    }

    @RequestMapping("/setObj")
    @ResponseBody
    public Object setObj() throws JsonProcessingException {
        return redisService.incr("zcblog:blog1", 1);
    }

    @RequestMapping("/dalTestAdd")
    @ResponseBody
    public ResultBO<Void> dalTest() {
        UserEntity userEntity = new UserEntity();
        userEntity.setName("lisi");
        userEntity.setPwd("lisi");
        userService.addUser(userEntity);
        return ResultBO.success();
    }

    @Autowired
    MessageSourceUtils messageSourceUtils;

    @RequestMapping("/test")
    public String test(String code) {
        return messageSourceUtils.getMessage(code);
    }

    @RequestMapping("/testException1")
    public ResultBO<Void> testException() {
        try {
            System.out.println(3 / 0);
        } catch (Exception ex) {
            throw new BaseException("1111", "");
        }
        return ResultBO.success();
    }

    @RequestMapping("/testException2")
    public ResultBO<Void> validatorTest() {
        try {
            System.out.println(3 / 0);
        } catch (Exception ex) {
            Validators.throwAnyway(BlogExceptionCode.BLOG_ERROR_01);
        }
        return ResultBO.success();
    }
}
